#!/usr/bin/env bash

SCRIPTDIR=`pwd -P`
SCRIPTDIR+="/`dirname $0`"
ISODIR=${LFS}/ISO
ARCH=`uname -m`
LABEL=yaolinuxcd
GRUB_EFI_ARCH="x86_64"
GRUB_EFI_IMG="bootx64.efi"
if [ "$ARCH" == "i686" ]; then
	GRUB_EFI_ARCH="i386"
	GRUB_EFI_IMG="bootia32.efi"
fi
error()	{
	echo "something went wrong: ${1}"
	exit 1
}


main() {
	if [ "${1}" == "-h" ] || [ "${1}" == "--help" ];then
		echo "
USAGE: mkiso [YaoLinuX-version] [kernel-version]

OPTIONS:

 Arguments order need to be respected:

   Arg1: [YaoLinuX-version]   The version of the YaoLinux to propose
   Arg2: [kernel-version]  Ovewrite the kernel version (return of uname -r command)
"
	exit 0
	fi

for param in "$@"; do
  case $param in
     --bootia32)
       bootia32=y
       i686iso=YaoLinuX_i686-9.1.iso
       ;;
     -bootia32=*)
       bootia32=y
       i686iso=${param#bootia32=}
       ;;
     *)
       newCmdLn=${newCmdLn}" "$param
       ;;
  esac
done
[ "$bootia32" == "y" ] && set -- $newCmdLn

	[ $UID -ne 0 ] && error "Run this command as root"
	[ -z "$LFS" ] && error "The LFS variable is not set"
	if [ -z "${2}" ]; then
		VERSION_KERNEL=`uname -r`
	else
		VERSION_KERNEL=${2}
	fi
	if [ -z "${1}" ]; then
		VERSION_YAOLINUX=`date +%Y%m%d`
	else
		VERSION_YAOLINUX=${1}
	fi
	[ ! -d "$LFS/lib/modules/${VERSION_KERNEL}" ] && error "It's not the ${VERSION_KERNEL} kernel in this ISO"

	[ ! -f "`which xorriso`" ] && error "You need to install 'libisoburn'"
	[ ! -f "`which isohybrid`" ] && error "You need to install 'syslinux'"
	[ ! -f "`which mkdosfs`" ] && error "You need to install 'dosfstools'"
	[ ! -d /usr/lib/grub/${GRUB_EFI_ARCH}-efi ] && error "You need to install 'grub-efi'"

	# isolinux stufs
	cd ${ISODIR} || error "cd ${ISODIR}"
	for FILE in splash.png libutil.c32 libcom32.c32 ldlinux.c32 vesamenu.c32 isolinux.bin
	do
		[ ! -f /usr/lib/syslinux/$FILE ] && error "syslinux not properly installed"
		cp -v /usr/lib/syslinux/$FILE isolinux/
	done

	ISO=YaoLinuX_${ARCH}-${VERSION_YAOLINUX}
	sed "s|#VERSION_YAOLINUX#|${VERSION_YAOLINUX}|" $SCRIPTDIR/../iso/isolinux/menu.cfg > isolinux/menu.cfg
	sed "s|#VERSION_KERNEL#|${VERSION_KERNEL}|" $SCRIPTDIR/../iso/isolinux/live.cfg > isolinux/live.cfg
	sed "s|#VERSION_KERNEL#|${VERSION_KERNEL}|" $SCRIPTDIR/../iso/isolinux/install.cfg > isolinux/install.cfg
	sed -i "s|#VERSION_KERNEL#|${VERSION_KERNEL}|" isolinux/menu.cfg

	# efi stufs
	[ -d iso ] && rm -rv iso
	[ -d boot/grub ] && rm -r boot/grub
	mkdir -pv {iso/EFI,boot/grub/{fonts,${GRUB_EFI_ARCH}-efi}}
	# grub
        if [ -f /usr/share/grub/unicode.pf2 ];then
		cp /usr/share/grub/unicode.pf2 boot/grub/fonts
        fi
	if [ -f isolinux/splash.png ];then
		mv isolinux/splash.png boot/grub
	fi
	echo "set prefix=/boot/grub" > iso/grub-early.cfg
	cp -av /usr/lib/grub/${GRUB_EFI_ARCH}-efi/*.mod /usr/lib/grub/${GRUB_EFI_ARCH}-efi/*.lst boot/grub/${GRUB_EFI_ARCH}-efi
	cat  > boot/grub/grub.cfg << "EOF"
set default=0
set timeout=99

# set debug=all
# set pager=1

insmod all_video
if loadfont /boot/grub/fonts/unicode.pf2; then
  set gfxmode=800x600
  insmod efi_gop
  insmod efi_uga
  insmod video_bochs
  insmod video_cirrus
  insmod gfxterm
  insmod png
  terminal_output gfxterm
fi

if background_image /boot/grub/splash.png; then
  set color_normal=light-gray/black
  set color_highlight=white/black
else
  set color_normal=cyan/blue
  set color_highlight=white/blue
fi

menuentry "YaoLinuX #ARCH# #VERSION_YAOLINUX# #VERSION_KERNEL# (UEFI mode) normal" {

    linux /boot/kernel-#VERSION_KERNEL#  ro quiet
    initrd /boot/initrd-#VERSION_KERNEL#
}
menuentry "YaoLinuX #ARCH# #VERSION_YAOLINUX# #VERSION_KERNEL# (UEFI mode) debug" {
    set background_color=black
    linux /boot/kernel-#VERSION_KERNEL#  ro testiso rootdelay=3
    initrd /boot/initrd-#VERSION_KERNEL#
}
menuentry "YaoLinuX #ARCH# #VERSION_YAOLINUX# #VERSION_KERNEL# (UEFI mode) installable live" {
    linux /boot/kernel-#VERSION_KERNEL#  ro quiet rootdelay=5 live
    initrd /boot/initrd-#VERSION_KERNEL#
}
menuentry "YaoLinuX #ARCH# #VERSION_YAOLINUX# #VERSION_KERNEL# (UEFI mode) debug live" {
    linux /boot/kernel-#VERSION_KERNEL#  ro testiso rootdelay=3 live
    initrd /boot/initrd-#VERSION_KERNEL#
}
EOF
	sed -i "s|#VERSION_YAOLINUX#|${VERSION_YAOLINUX}|" boot/grub/grub.cfg
	sed -i "s|#VERSION_KERNEL#|${VERSION_KERNEL}|" boot/grub/grub.cfg
	sed -i "s|#ARCH#|${ARCH}|" boot/grub/grub.cfg

	for FILE in kernel initrd
	do
		[ -f isolinux/$FILE ] && mv -v isolinux/$FILE boot/
	done
	grub-mkimage-efi -c iso/grub-early.cfg -o iso/EFI/${GRUB_EFI_IMG} -O ${GRUB_EFI_ARCH}-efi -p "" iso9660 normal \
	search search_fs_file || error "grub-mkimage"

	# efiboot.img
	modprobe loop
	dd if=/dev/zero of=iso/efiboot.img count=4096
	mkdosfs -n yaolinuxcd iso/efiboot.img || error "mkdosfs"
	mkdir -pv iso/efiboot
	mount -o loop iso/efiboot.img iso/efiboot || error "mount efiboot.img"
	mkdir -pv iso/efiboot/EFI/boot
	cp -v iso/EFI/${GRUB_EFI_IMG} iso/efiboot/EFI/boot

  [ "$bootia32" == "y" ] && ia32

	umount iso/efiboot

	# Iso generation
	xorriso -as mkisofs -R -l -J -o ${LFS}/${ISO}.iso \
	-A YAOLINUX \
	-b isolinux/isolinux.bin \
	-c isolinux/boot.cat -no-emul-boot -boot-load-size 4 \
	-boot-info-table \
	-eltorito-alt-boot -e iso/efiboot.img -no-emul-boot \
	-input-charset utf-8 -V "${LABEL}" .

	cd ${LFS}
	isohybrid -u ${ISO}.iso || error "isohybrid ${ISO}.iso"
	sha256sum ${ISO}.iso > ${ISO}.sha256sum
}

ia32() {
   # to boot some tablets with 32-bits UEFI and 64-bits processor
   if [ "$ARCH" == "x86_64" ]; then
      if [ ! -f iso/EFI/bootia32.efi ]; then
         [ -f /usr/bin/iso-read ] || error "please install libcdio"
         [ -d $LFS/WORK ] || mkdir -pv $LFS/WORK
         [ -f $LFS/WORK/${i686iso} ] || wget http://downloads.yaolinux.org/${i686iso} -O $LFS/WORK/${i686iso}
         iso-read -e /iso/EFI/bootia32.efi $LFS/WORK/${i686iso} -o iso/EFI/bootia32.efi
      fi
      cp -v iso/EFI/bootia32.efi iso/efiboot/EFI/boot
      TS=`wget http://downloads.yaolinux.org/i686/development/base/grub-efi/.PKGREPO -O - | head -n 1 | cut -d'#' -f1`
      GE=grub-efi${TS}i686.cards.tar.xz
      mkdir -p $LFS/WORK
      [ -f $LFS/WORK/$GE ] || wget http://downloads.yaolinux.org/i686/development/base/grub-efi/$GE -O $LFS/WORK/$GE || error "wget grub-efi i686"
      tar -C boot -xvf $LFS/WORK/$GE --strip-components=2 --wildcards *.mod || error "tar -C boot $GE"
   else
      error "--bootia32 option is not available for $ARCH architecture"
   fi
}

main "$@"
